﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using PMS.Entity;
using PMS.Resources;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.Win
{
    public partial class FormPopupBase : BaseForm
    {
        public FormPopupBase()
        {
            InitializeComponent();
        }

        #region Form Event

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            SetEventHandler(this);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            //do nothing
        }

        protected override void PreSetupProperties(Control control)
        {
            base.PreSetupProperties(control);
            if (control is GridControl)
            {
                foreach (GridView item in (control as GridControl).ViewCollection)
                {
                    item.OptionsCustomization.AllowSort = true;
                    item.OptionsCustomization.AllowFilter = true;
                }
            }
        }
        #endregion

        #region Private functions
        private void SetEventHandler(Control control)
        {
            PreSetupProperties(control);

            if ((control is TextEdit) ||
               (control is ComboBoxEdit) ||
               (control is DateEdit) ||
               (control is ButtonEdit))
            {

                (control as TextEdit).Enter += (sender, e) =>
                {
                    (control as TextEdit).SelectAll();
                };
            }
            else if ((control is RadioButton) ||
               (control is CheckEdit))
            {

                (control as CheckEdit).KeyPress += (sender, e) =>
                {
                    if (e.KeyChar == 13)
                    {
                        if (SelectNextControl(control, true, true, true, true) == false)
                        {
                            SelectNextControl(control, false, true, true, true);
                        }
                    }
                };
            }
            else if (control is XtraTabControl)
            {
                (control as XtraTabControl).KeyPress += (sender, e) =>
                {
                    if (e.KeyChar == 13)
                    {
                        XtraTabPage page = (control as XtraTabControl).SelectedTabPage;
                        if (page != null)
                        {
                            if (SelectNextControl(page, true, true, true, true) == false)
                            {
                                SelectNextControl(page, false, true, true, true);
                            }
                        }
                    }
                };
            }
            else if (control is GridControl)
            {
                (control as GridControl).ProcessGridKey += (object sender, System.Windows.Forms.KeyEventArgs e) =>
                {
                    if (e.KeyCode == Keys.Tab)
                    {
                        GridControl grid = sender as GridControl;
                        GridView view = grid.FocusedView as GridView;
                        if ((e.Modifiers == Keys.None && view.IsLastRow && view.FocusedColumn.VisibleIndex ==
                             view.VisibleColumns.Count - 1)
                            || (e.Modifiers == Keys.Shift && view.IsFirstRow && view.FocusedColumn.VisibleIndex == 0) || view.RowCount == 0)
                        {
                            if (view.IsEditing)
                                view.CloseEditor();

                            SelectNextControl(grid, e.Modifiers == Keys.None, true, true, true);
                            e.Handled = false;
                        }
                    }
                };

                (control as GridControl).GotFocus += (object sender, EventArgs e) =>
                {
                    GridControl grid = sender as GridControl;
                    GridView view = grid.FocusedView as GridView;
                    if (view.OptionsView.ShowAutoFilterRow)
                    {
                        view.FocusedRowHandle = GridControl.AutoFilterRowHandle;
                    }
                };
            }


            foreach (Control child in control.Controls)
            {
                SetEventHandler(child);
            }
        }
        #endregion

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            FirstFocusGrid(this);
        }

        private void FirstFocusGrid(Control control)
        {
            if (control is GridControl)
            {
                (control as GridControl).Focus();
                return;
            }

            foreach (Control child in control.Controls)
            {
                FirstFocusGrid(child);
            }
        }
    }
}